package servlet;

import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import  dao.userDao;
import utils.JwtUtil;

@WebServlet("/api/login.do")
public class Login extends BaseServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("name1");
        String password=req.getParameter("pwd1");
        System.out.println(username+"//"+password);

        User a=new User();
        a.setuName(username);
        a.setuPassWord(password);
        userDao b=new userDao();
        int num=0;
        num= b.find(a);
        if (num>0){
            // 定义一个Map对象，用于封装相关的令牌
            Map<String, Object> map = new HashMap<>();
            // 封装数据 -- 客户端浏览器只保留帐号名（用户ID）即可
            map.put("username", username);
            map.put("uid",num);
            // 创建Token,设置有效时间为5分钟
            String token=JwtUtil.createToken(map,5*24*60*60*1000);
//            String token = JwtUtil.createToken(map, 5*24*60* 60 * 1000);
            User.setUID(num);
            // 响应客户端
            print(resp, successJson(token));

            return;
        }

    }
}
